//
//  UIView+Additions.m
//  HTF_New
//
//  Created by lijinyang on 16/9/2.
//  Copyright © 2016年 htffund. All rights reserved.
//

#import "UIView+Additions.h"

@implementation UIView (Additions)
+(CGFloat)size_1px
{
    CGFloat scale_screen =[UIScreen mainScreen].scale;
    
    if (scale_screen>=2.0f)
    {
        return 1.0/scale_screen;
    }
    else if(scale_screen < 2.0f)
    {
        return 1.0f;
    }
    
    return 1.0f;
}

UIView * bgViewColorSeparateLine(CGRect frame)
{
    UIView *vLine = [[UIView alloc ] initWithFrame:frame];
    //    vLine.tag = 1000 + frame.origin.y;
    vLine.backgroundColor = lWhiteColor;
    return vLine;
}

UIView * separateLine(CGRect frame)
{
    UIView *vLine = [[UIView alloc ] initWithFrame:frame];
//    vLine.tag = 1000 + frame.origin.y;
    vLine.backgroundColor = lWhiteColor;
    return vLine;
}

UIView * garySeparateLine(CGRect frame)
{
    UIView *vLine = [[UIView alloc ] initWithFrame:frame];
    //    vLine.tag = 1000 + frame.origin.y;
    vLine.backgroundColor = lWhiteColor;
    return vLine;
}

UIView * redSeparateLine(CGRect frame)
{
    UIView *vLine = [[UIView alloc ] initWithFrame:frame];
    
    if (frame.size.height ==[UIView size_1px])
    {
        vLine.backgroundColor =lWhiteColor;
    }
    else
    {
        vLine.backgroundColor =[UIColor colorWithRed:1 green:1 blue:1 alpha:0.5];
    }
    return vLine;
}

UIView * separateLineWithColor(CGRect frame,UIColor *lineColor){
    UIView *vLine = [[UIView alloc ] initWithFrame:frame];
    
    if (lineColor)
    {
        vLine.backgroundColor =lineColor;
    }
    else{
        vLine.backgroundColor =lWhiteColor;
    }
    
    return vLine;
}

//画虚线
UIView * drawDotLine(CGRect frame ,UIView* superView,UIColor *lineColor)
{
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:frame];
    [superView addSubview:imageView1];

    UIGraphicsBeginImageContext(imageView1.frame.size);   //开始画线
    [imageView1.image drawInRect:CGRectMake(0, 0, imageView1.frame.size.width, imageView1.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);  //设置线条终点形状


    CGFloat lengths[] = {3.0,2.0};
    CGContextRef line = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(line, lineColor?lineColor.CGColor:lBlackColor.CGColor);

    CGContextSetLineDash(line, 0, lengths, 2);  //画虚线
    CGContextMoveToPoint(line, 0, 1.0); //开始画线
    CGContextAddLineToPoint(line, frame.size.width, 1.0);
    CGContextStrokePath(line);

    imageView1.image = UIGraphicsGetImageFromCurrentImageContext();
    return imageView1;
}
/**
 *  通过 CAShapeLayer 方式绘制虚线
 *
 *  param lineView:       需要绘制成虚线的view
 *  param lineLength:     虚线的宽度
 *  param lineSpacing:    虚线的间距
 *  param lineColor:      虚线的颜色
 *  param lineDirection   虚线的方向  YES 为水平方向， NO 为垂直方向
 **/
- (void)drawLineOfDashByCAShapeLayer:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor lineDirection:(BOOL)isHorizonal {

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    [shapeLayer setBounds:lineView.bounds];

    if (isHorizonal) {

        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];

    } else{
        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame)/2)];
    }

    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    if (isHorizonal) {
        [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    } else {

        [shapeLayer setLineWidth:CGRectGetWidth(lineView.frame)];
    }
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);

    if (isHorizonal) {
        CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
    } else {
        CGPathAddLineToPoint(path, NULL, 0, CGRectGetHeight(lineView.frame));
    }

    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [self.layer addSublayer:shapeLayer];
}
-(void)setBorderWithColor:(UIColor *)borderColor
{
    self.layer.borderColor =borderColor.CGColor;
    self.layer.borderWidth =[UIView size_1px];
}

-(UIImage *)selfChangeToImage {
    
   CGSize shotSize = self.frame.size;
   
   if ([self isKindOfClass:[UITableView class]]) {
      shotSize =((UITableView *)self).contentSize;
      self.layer.frame =CGRectMake(0, 0, shotSize.width, shotSize.height);
   }
   else if ([self isKindOfClass:[UIScrollView class]]){
      shotSize =((UIScrollView *)self).contentSize;
      self.layer.frame =CGRectMake(0, 0, shotSize.width, shotSize.height);
   }
   
    UIGraphicsBeginImageContextWithOptions(shotSize, NO, [UIScreen mainScreen].scale);

    [self.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}


- (void)setAllCornersWithCornerRadius:(float)cornerRadius
{
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = YES;
}
- (void)setCorners:(UIRectCorner)corners cornerRadius:(float)cornerRadius{
   if (!self.superview) {
      return;
   }
   
   if (self.frame.size.width>0&&
       self.frame.size.height>0) {
      CAShapeLayer *shapeLayer = [CAShapeLayer layer];
      shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)].CGPath;
      self.layer.mask = shapeLayer;
   }
}

-(UIViewController *)findViewControllerResponder{
    UIView *responder =self;

    while (![responder isKindOfClass:[UIViewController class]]&&
           responder) {
        responder = responder.nextResponder;
    }

    return responder;
}

#pragma mark 展示缺省页




+(void)viewShadowPathWithView:(UIView*)view Color:(UIColor *)shadowColor shadowOpacity:(CGFloat)shadowOpacity shadowRadius:(CGFloat)shadowRadius shadowPathType:(NSInteger)shadowPathType shadowPathWidth:(CGFloat)shadowPathWidth{
    
    view.layer.masksToBounds = NO;//必须要等于NO否则会把阴影切割隐藏掉
    view.layer.shadowColor = shadowColor.CGColor;// 阴影颜色
    view.layer.shadowOpacity = shadowOpacity;// 阴影透明度，默认0
    view.layer.shadowOffset = CGSizeZero;//shadowOffset阴影偏移，默认(0, -3),这个跟shadowRadius配合使用
    view.layer.shadowRadius = shadowRadius;//阴影半径，默认3
    CGRect shadowRect = CGRectZero;
    CGFloat originX,originY,sizeWith,sizeHeight;
    originX = 0;
    originY = 0;
    sizeWith = view.frame.size.width;
    sizeHeight = view.frame.size.height;
    
    if (shadowPathType == 0) {
        shadowRect = CGRectMake(originX, originY-shadowPathWidth/2, sizeWith, shadowPathWidth);
    }else if (shadowPathType == 1){
        shadowRect = CGRectMake(originY, sizeHeight-shadowPathWidth/2, sizeWith, shadowPathWidth);
    }else if (shadowPathType == 2){
        shadowRect = CGRectMake(originX-shadowPathWidth/2, originY, shadowPathWidth, sizeHeight);
    }else if (shadowPathType == 3){
        shadowRect = CGRectMake(sizeWith-shadowPathWidth/2, originY, shadowPathWidth, sizeHeight);
    }
              
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:shadowRect];
    view.layer.shadowPath = bezierPath.CGPath;//阴影路径
}


/**
 设置渐变色
 startColor:开始颜色
 endColor:结束颜色
 horizonalDirection: yes 水平 no垂直
 @6.70
 */
-(void)setGriediantLayerWithStartColor:(UIColor *)startColor endColor:(UIColor *)endColor isHorizonalDirection:(BOOL)horizonalDirection{
    for (CAGradientLayer *layer in self.layer.sublayers) {
        if ([layer isKindOfClass:[CAGradientLayer class]]) {
            [layer removeFromSuperlayer];
            break;
        }
    }
   
    CAGradientLayer *gradient = [[CAGradientLayer alloc] init];
    gradient.frame = self.bounds;
    gradient.colors = @[(id)startColor.CGColor,(id)endColor.CGColor];
    gradient.locations = @[@(0), @(1.0)];
    if (horizonalDirection)
    {
        gradient.startPoint = CGPointMake(0, 0);
        gradient.endPoint = CGPointMake(1, 0);
    }
    else
    {
        gradient.startPoint = CGPointMake(0, 0);
        gradient.endPoint = CGPointMake(0, 1);
    }
    [self.layer insertSublayer:gradient atIndex:0];
}

- (UIImage *)nomalSnapshotImage
{
   UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, [UIScreen mainScreen].scale);
   [self.layer renderInContext:UIGraphicsGetCurrentContext()];
   UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   return snapshotImage;
}



-(UIView *)findTargetSuperViewClassName:(NSString *)className{
   if (!className.length) {
      return NO;
   }
   
   UIView *view =self;
   
   do {
      view =view.superview;
   } while (view&&![NSStringFromClass(view.class) isEqualToString:className]);
   

   return view;
}

-(void)removeAllSubView{
   [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
      [obj removeFromSuperview];
   }];
}
@end
